package com.szbber.commen.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment

//持有俩个泛型，VM和DB，分别是BaseViewModel和ViewDataBinding的实现类
abstract class BaseVMFragment<V : ViewDataBinding, VM : BaseViewModel> : Fragment() {

    //是否打印生命周期
    var openLifecycle: Boolean = false
    protected lateinit var mBinding: V
    protected lateinit var mViewModel: VM
    private var contentView: View? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(savedInstanceState)
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (userVisibleHint) { //fragment可见

        } else { //fragment不可见

        }
    }

    override fun onCreateView(inflater: LayoutInflater,
        container: ViewGroup?,
        state: Bundle?): View? {
        if(contentView!= null){
            return contentView
        }else {
            mBinding = DataBindingUtil.inflate(inflater, getLayoutResId(), container, false)
            mBinding.lifecycleOwner = this
            mViewModel = initVM()
            contentView = mBinding.root
            return contentView
        }
    }

    abstract fun getLayoutResId(): Int
    abstract fun initView(savedInstanceState: Bundle?)
    abstract fun initVM(): VM

    override fun onResume() {
        super.onResume() //        LogUtil.e(TAG, "onResume", openLifecycle)
    }

    override fun onPause() {
        super.onPause() //        LogUtil.e(TAG, "onPause", openLifecycle)
    }

    override fun onStart() {
        super.onStart() //        LogUtil.e(TAG, "onStart", openLifecycle)
    }

    override fun onStop() {
        super.onStop() //        LogUtil.e(TAG, "onStop", openLifecycle)
    }

    override fun onDestroy() {
        super.onDestroy() //        LogUtil.e(TAG, "onDestroy", openLifecycle)
    }


}